<?php

namespace app\batch;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\models\{MachineLog};
use think\Db;
use app\facade\Redis;
use think\facade\Debug;


class PoolList extends Command
{

    protected function configure()
    {
        $this->setName('PoolList')->setDescription('矿机收益释放');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->doCron();
        die();
    }

    protected function doCron()
    {
        $cmd = "ps aux | grep '/www/server/php/72/bin/php /www/wwwroot/JTE/think Jtequotes'|grep -v 'grep'| awk '{print $2}'";
        exec($cmd, $ps, $ret);
        if (count($ps) > 1) {
            die("batch of  is running");
        }
        ini_set('max_execution_time', 0);
        (new MachineLog())->where(['status' => 1])->where('days', '<', date('Y-m-d'))->chunk(100, function ($lists) {
            foreach ($lists as $list) {
                if ($list['lday'] > 1) {
                    $income = $list['ddividends'];
                    (new MachineLog())->where('id', $list['id'])
                        ->update(['lday' => ($list['lday'] - 1), 'allincome' => ($list['allincome'] + $income), 'todayincome' => $income, 'income' => ($list['income'] + $income), 'days' => date('Y-m-d H:i:s')]);
                } elseif ($list['lday'] == 1) {
                    $income = $list['dividends'] - ($list['day'] - 1) * $list['ddividends'];
                    $income = $income < 0 ? 0 : $income;
                    (new MachineLog())->where('id', $list['id'])
                        ->update(['status' => 2, 'lday' => 0, 'allincome' => ($list['allincome'] + $income), 'todayincome' => $income, 'income' => ($list['income'] + $income), 'days' => date('Y-m-d H:i:s')]);
                } else {
                    (new MachineLog())->where('id', $list['id'])
                        ->update(['status' => 2, 'days' => date('Y-m-d H:i:s')]);
                }
            }
        });
    }
}
